{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 退火算法的初始值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100:2738.000000\n",
      "200:2591.000000\n",
      "500:2591.000000\n",
      "1000:2591.000000\n",
      "2000:2591.000000\n",
      "5000:2591.000000\n",
      "10000:2591.000000\n",
      "20000:2591.000000\n",
      "50000:2591.000000\n"
     ]
    }
   ],
   "source": [
    "import optimization\n",
    "\n",
    "domain = [(0, 9)] * (len(optimization.people) * 2)\n",
    "for t in [100,200,500,1000,2000,5000,10000,20000,50000]:\n",
    "    best = 99999\n",
    "    for i in range(20):\n",
    "        s = optimization.annealingoptimize(domain, optimization.schedulecost, T=t)\n",
    "        cost = optimization.schedulecost(s)\n",
    "        if cost<best:\n",
    "            best = cost\n",
    "        \n",
    "    print(\"%d:%f\"%(t,best))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  总体来看，初始温度越高，迭代次数越多，得到的解的平均质量也越好，但几乎都能得到全局最优解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 遗传优化算法的结束条件\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<----Genetic Algorithms---->\n",
      "4363\n",
      "3507\n",
      "3379\n",
      "3369\n",
      "3150\n",
      "3150\n",
      "2994\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "2912\n",
      "[5, 3, 5, 3, 4, 3, 3, 0, 0, 0, 0, 0]\n",
      "Present cost:2912.000000\n",
      "   Seymour       BOS 13:40-15:37 $138 10:33-12:03 $ 74\n",
      "    Franny       DAL 10:30-14:57 $290 14:20-17:32 $332\n",
      "     Zooey       CAK 13:40-15:38 $137 10:32-13:16 $139\n",
      "      Walt       MIA 11:28-14:40 $248 12:37-15:05 $170\n",
      "     Buddy       ORD 12:44-14:17 $134 10:33-13:11 $132\n",
      "       Les       OMA 11:08-13:07 $175 11:07-13:24 $171\n"
     ]
    }
   ],
   "source": [
    "# # exercise 3 : 遗传优化算法的结束条件\n",
    "# if costf(sum(pop) == sum(scores[0:topelite])):\n",
    "#     break\n",
    "\n",
    "print('\\n<----Genetic Algorithms---->')\n",
    "s = optimization.geneticoptimize(domain, optimization.schedulecost)\n",
    "print(s)\n",
    "cost = optimization.schedulecost(s)\n",
    "print('Present cost:%f' % cost)\n",
    "optimization.printschedule(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学生组队"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# (0,9),(0,7),(0,5),(0,3),(0,1)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
